﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

using NinjaToolbox.PhysicsSystem;

[CustomEditor(typeof(ColliderObject))]
public class ColliderObjectInspector : Editor
{
    OBBBoxSceneGUIDrawer mObbBoxSceneGUIDrawer;
    OBBBoxSceneGUIDrawer ObbBoxSceneGUIDrawer
    {
        get
        {
            if (mObbBoxSceneGUIDrawer == null)
            {
                mObbBoxSceneGUIDrawer = new OBBBoxSceneGUIDrawer();
                var colliderObject = target as ColliderObject;
                mObbBoxSceneGUIDrawer.Initialization(() => colliderObject.ColliderShape);

                mObbBoxSceneGUIDrawer.OnObbBoxChanged += (obb) =>
                {
                    (target as ColliderObject).SetColliderShape(obb);
                };
            }

            return mObbBoxSceneGUIDrawer;
        }
    }


    void Awake()
    {
        ObbBoxSceneGUIDrawer.GetHashCode();
    }

    void OnDestroy()
    {
        ObbBoxSceneGUIDrawer.Dispose();
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        var shapeOffsetProp = serializedObject.FindProperty("mColliderShape.offset");
        var shapeSizeProp = serializedObject.FindProperty("mColliderShape.size");

        var isDrawGizmosProp = serializedObject.FindProperty("isDrawGizmos");
        var alwaysVisibleGizmosProp = serializedObject.FindProperty("alwaysVisibleGizmos");

        using (var changeCheck = new EditorGUI.ChangeCheckScope())
        {
            ObbBoxSceneGUIDrawer.DrawInspectorGUI();

            EditorGUILayout.PropertyField(shapeOffsetProp);
            EditorGUILayout.PropertyField(shapeSizeProp);
            EditorGUILayout.PropertyField(isDrawGizmosProp);
            EditorGUILayout.PropertyField(alwaysVisibleGizmosProp);

            if (changeCheck.changed)
            {
                GUI.changed = true;
                serializedObject.ApplyModifiedProperties();
            }
        }
    }
}
